/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package br.com.maternity.classes.dao;

import br.com.maternity.classes.entity.Atendimento;
import java.sql.PreparedStatement;
import java.util.ArrayList;
import java.util.Date;
import java.util.List;

/**
 *
 * @author Gattini
 */
public class AtendimentoDAO extends GenericDAO<Atendimento> {

    private static Atendimento instance = new Atendimento();

    public AtendimentoDAO() {
        super(instance);
    }

    @Override
    public List<GenericDAOField> listFields() {
        List l = new ArrayList<GenericDAOField>();

        l.add(new GenericDAOField(
                "Id", "setId", "getId", Integer.class, true, GenericDAOFieldPrimaryKey.IDENTITY));
        l.add(new GenericDAOField(
                "DataAtendimento", "setDataAtendimento", "getDataAtendimento", Date.class));
        l.add(new GenericDAOField(
                "IdUnidade", "setIdUnidade", "getIdUnidade", Integer.class));
        l.add(new GenericDAOField(
                "ProntuarioPaciente", "setProntuarioPaciente", "getProntuarioPaciente", Integer.class));
        l.add(new GenericDAOField(
                "Situacao", "setSituacao", "getSituacao", Integer.class));
        l.add(new GenericDAOField(
                "Prioridade", "setPrioridade", "getPrioridade", Integer.class));
        l.add(new GenericDAOField(
                "DiagnosticoInicial", "setDiagnosticoInicial", "getDiagnosticoInicial", String.class));
        l.add(new GenericDAOField(
                "CNSClinica", "setCnsClinica", "getCnsClinica", Integer.class));
        l.add(new GenericDAOField(
                "CodigoProcedimento", "setCodigoProcedimento", "getCodigoProcedimento", String.class));
        l.add(new GenericDAOField(
                "CaraterInternacao", "setCaraterInternacao", "getCaraterInternacao", String.class));
        l.add(new GenericDAOField(
                "CondicaoInternacao", "setCondicaoInternacao", "getCondicaoInternacao", String.class));
        l.add(new GenericDAOField(
                "Sintomas", "setSintomas", "getSintomas", String.class));
        l.add(new GenericDAOField(
                "QueixasPaciente", "setQueixasPaciente", "getQueixasPaciente", String.class));
        l.add(new GenericDAOField(
                "PAMinima", "setPaMinima", "getPaMinima", Integer.class));
        l.add(new GenericDAOField(
                "PAMaxima", "setPaMaxima", "getPaMaxima", Integer.class));
        l.add(new GenericDAOField(
                "CRMMedico", "setCrmMedico", "getCrmMedico", Integer.class));
        l.add(new GenericDAOField(
                "DataTriagem", "setDataTriagem", "getDataTriagem", Date.class));
        l.add(new GenericDAOField(
                "DataConsulta", "setDataConsulta", "getDataConsulta", Date.class));
        return l;
    }

    @Override
    public String tableName() {
        return "Atendimento";
    }

    public Atendimento getById(Integer id) {
        try {
            String query = "SELECT * FROM " + tableName()
                    + " WHERE Id = ?";

            PreparedStatement ps = getPreparedStatement(query);
            ps.setInt(1, id);
            return select(ps);
        } catch (Exception ex) {
            return null;
        }
    }

    public Atendimento getAtendimentoAbertoPaciente(Integer prontuario) {
        try {
            String query = "SELECT * FROM " + tableName()
                    + " WHERE Situacao <> 9 AND ProntuarioPaciente = ?";
            PreparedStatement ps = getPreparedStatement(query);
            ps.setInt(1, prontuario);
            return select(ps);
        } catch (Exception ex) {
            return null;
        }
    }

    public List<Atendimento> selectByStatus(Integer status) {
        try {
            String query = "SELECT * FROM " + tableName()
                    + " WHERE Situacao = ? ORDER BY";
            switch (status) {
                case 2:
                    query += " Prioridade DESC, DataTriagem";
                    break;
                case 3:
                    query += " DataConsulta";
                    break;
                default:
                    query += " DataAtendimento";
                    break;
            }

            PreparedStatement ps = getPreparedStatement(query);
            ps.setInt(1, status);
            return selectList(ps);
        } catch (Exception ex) {
            return new ArrayList<Atendimento>();
        }
    }
    
    
}
